/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import org.evswork.getwit.smackwrap.Talk;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;

public class RequestStatusDialogBuilder extends Builder
			implements OnMultiChoiceClickListener, OnClickListener {
	UserNameSet users;
	GttMainActivity act;
	
	// store data for dialog
	UserName [] mUsers;
	boolean [] mChoices;
	int mSize;
	
	public RequestStatusDialogBuilder(Context ctxt,TweetObject data) {
		super(ctxt);
		users = new UserNameSet();
		act = (GttMainActivity)ctxt;
		users.add(data);
	}
	
	public void DoWork() {
		mSize = users.size();
		mUsers = users.toArray(new UserName[mSize]);
		mChoices = new boolean[mSize];
		for (int i=0;i<mSize;++i)
			mChoices[i] = true;
		
		if (mSize == 1)
		{
			doFinalWork();
			return;
		}
		
		this
			.setTitle("需要查看的用户:")
			.setMultiChoiceItems(mUsers, mChoices, this)
			.setPositiveButton("确定", this)
			.setNegativeButton("取消", null)
			.show();
	}
	
	public void doFinalWork() {
		for (int i=0;i<mSize;++i) {
			if (mChoices[i]) {
				Talk.send("-st "+mUsers[i]);
				act.processMessage(TweetObject.makeDummyUserData(act, mUsers[i].n));
			}
		}
		act.showHomeLayout();
	}
	
	@Override
	public void onClick(DialogInterface dialog, int who) {
		switch (who) {
		case DialogInterface.BUTTON_POSITIVE:
			doFinalWork();
			return;
		}
	}

	@Override
	public void onClick(DialogInterface dialog, int which, boolean isChecked) {
		mChoices[which]=isChecked;
	}
	
}
